package com.rgs.gateway.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.rgs.gateway.admin.entity.dto.GatewayRouteQueryParam;
import com.rgs.gateway.admin.entity.po.GatewayRoutePO;
import com.rgs.gateway.admin.entity.vo.GatewayRouteCreateReqVO;
import com.rgs.gateway.admin.entity.vo.GatewayRouteQueryReqVO;
import com.rgs.gateway.admin.entity.vo.GatewayRouteRespVO;
import com.rgs.gateway.admin.service.IGatewayRouteService;
import com.rgs.gateway.common.pojo.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author rgs
 * @deprecated 路由配置表的CRUR以及刷新
 */
@RestController
@RequestMapping("/gateway/routes")
@Slf4j
public class GatewayRouteController {

    @Autowired
    private IGatewayRouteService gatewayRoutService;

    @ApiOperation(value = "新增网关路由", notes = "新增一个网关路由")
    @PostMapping
    public Result add(@Valid @RequestBody GatewayRouteCreateReqVO gatewayRoutForm) {
        log.info("name:", gatewayRoutForm);
        GatewayRoutePO gatewayRout = BeanUtil.copyProperties(gatewayRoutForm, GatewayRoutePO.class);
        return Result.success(gatewayRoutService.add(gatewayRout));
    }

    @ApiOperation(value = "删除网关路由", notes = "根据url的id来指定删除对象")
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(gatewayRoutService.delete(id));
    }

    @ApiOperation(value = "修改网关路由", notes = "修改指定网关路由信息")
    @PutMapping(value = "/{id}")
    public Result update(@PathVariable Long id, @Valid @RequestBody GatewayRouteCreateReqVO gatewayRoutForm) {
        GatewayRoutePO gatewayRout = BeanUtil.copyProperties(gatewayRoutForm, GatewayRoutePO.class);
        gatewayRout.setId(id);
        return Result.success(gatewayRoutService.update(gatewayRout));
    }

    @ApiOperation(value = "根据id获取网关路由", notes = "根据id获取指定网关路由信息")
    @GetMapping(value = "/{id}")
    public Result<GatewayRouteRespVO> get(@PathVariable String id) {
        log.info("get with id:{}", id);
        return Result.success(BeanUtil.copyProperties(gatewayRoutService.get(id), GatewayRouteRespVO.class));
    }

    @ApiOperation(value = "根据uri获取网关路由", notes = "根据uri获取网关路由信息，简单查询")
    @GetMapping
    public Result getByUri(@RequestParam String uri) {
        return Result.success(gatewayRoutService.query(GatewayRouteQueryParam.builder().uri(uri).build()).stream().findFirst());
    }

    @ApiOperation(value = "搜索网关路由", notes = "根据条件查询网关路由信息")
    @PostMapping(value = "/conditions")
    public Result<GatewayRouteRespVO> search(@Valid @RequestBody GatewayRouteQueryReqVO gatewayRouteQueryReqVO) {
        GatewayRouteQueryParam oapr = BeanUtil.copyProperties(gatewayRouteQueryReqVO, GatewayRouteQueryParam.class);
        return Result.success(gatewayRoutService.query(oapr));
    }

    /**
     * 将数据库中配置的路由信息刷新到gateway服务中，并存入redis
     * @return
     */
    @ApiOperation(value = "重载网关路由", notes = "将所以网关的路由全部重载到redis中")
    @GetMapping(value = "/overload")
    public Result<Boolean> overload() {
        return Result.success(gatewayRoutService.overload());
    }

}